package fe;
import java.net.SocketException;
import java.net.UnknownHostException;

import udp.message.*;
import libraryCORBA.libraryCommandPOA;

public class FrontEndImpl extends libraryCommandPOA{

	private FEUdp FEudp;
	
	public FrontEndImpl(String id, int port){
			FEudp = new FEUdp(id,port);
	
	}
	
	@Override
	public String createAccount(String fn, String ln, String email,
			String phone, String un, String pw, String ei) {
		System.out.println("Front-End Impl level: Got Request from client: Create Account");
		CreateAccountMessage message = new CreateAccountMessage(fn, ln, email, phone, un, pw, ei);
		FEudp.sendToSequencer(message);
		while(true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String result = FEudp.getResult();
			if (result != null) return result;
		}
	}

	@Override
	public String reserveBook(String un, String pw, String book, String author,String ei) {
		System.out.println("Front-End Impl level: Got Request from client: Reserve Book");
		ReserveBookMessage message = new ReserveBookMessage(un,pw,book,author,ei);
		FEudp.sendToSequencer(message);
		while(true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String result = FEudp.getResult();
			if (result != null) return result;
		}
	}

	@Override
	public String getNonReturners(String un, String pw, int numDays, String ei) {
		System.out.println("Front-End Impl level: Got Request from client: Get Non Returner");
		GetNonReturnersMessage message = new GetNonReturnersMessage(un,pw,numDays,ei);
		FEudp.sendToSequencer(message);
		while(true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String result = FEudp.getResult();
			if (result != null) return result;
		}
	}

	@Override
	public String setDuration(int numDays, String username, String bookname,String ei) {
		System.out.println("Front-End Impl level: Got Request from client: Set Duration");
		SetDurationMessage message = new SetDurationMessage(numDays,username,bookname,ei);
		FEudp.sendToSequencer(message);
		while(true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String result = FEudp.getResult();
			if (result != null) return result;
		}
	}

	@Override
	public String reserveInterLibrary(String username, String password,
			String bookName, String authorName,String ei) {
		System.out.println("Front-End Impl level: Got Request from client: Reserver Inter Library");
		ReserveBookInterMessage message = new ReserveBookInterMessage(username,password,
				bookName, authorName,ei);
		FEudp.sendToSequencer(message);
		while(true) {
			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			String result = FEudp.getResult();
			if (result != null) return result;
		}
	}

}
